import pandas as pd
from pyecharts import options as opts
from pyecharts.charts import Pie,Line,Bar
import numpy as np
# 特征最影响结果的K个特征
from sklearn.feature_selection import SelectKBest
# 卡方检验，作为SelectKBest的参数
from sklearn.feature_selection import chi2


def taitan_demo():
    df = pd.read_csv("../datas/titanic/titanic_train.csv")
    df = df[["PassengerId", "Survived", "Pclass", "Sex", "Age", "SibSp", "Parch", "Fare", "Embarked"]].copy()
    df.head()
    # 3、数据清理和转换
    # 3.1 查看是否有空值列¶
    # print(df.info())
    # 3.2 给Age列填充平均值¶
    df['Age'] = df['Age'].fillna(df['Age'].median())
    # 3.2 将性别列变成数字
    # df.Sex.unique()
    df.loc[df['Sex']=="male",'Sex'] = 0
    df.loc[df['Sex']=="female",'Sex'] = 1
    # 3.3 给Embarked列填充空值，字符串转换成数字
    # Embarked
    df.Embarked.unique()
    # 填充空值
    df['Embarked'] = df['Embarked'].fillna(0)
    # 字符串变成数字
    df.loc[df["Embarked"] == "S", "Embarked"] = 1
    df.loc[df["Embarked"] == "C", "Embarked"] = 2
    df.loc[df["Embarked"] == "Q", "Embarked"] = 3

    # 4、将特征列和结果列拆分开
    y = df.pop('Survived')
    x = df

    # 5、使用卡方检验选择topK的特征
    # 选择所有的特征，目的是看到特征重要性排序
    bestfeatures = SelectKBest(score_func=chi2,k=len(x.columns))
    fit = bestfeatures.fit(x,y)

    # 6、按照重要性顺序打印特征列表
    df_score = pd.DataFrame(fit.scores_)
    # print(df_score)
    df_columns = pd.DataFrame(x.columns)
    # print(df_columns)
    # print(x.head())
    # 合并两个df
    new = pd.concat([df_columns,df_score],axis=1)
    #naming the dataframe columns
    new.columns = ['feature_name','Score']
    new.sort_values(by="Score", ascending=False)
    print(new)

if __name__ == "__main__":
    taitan_demo()